<?php

namespace Admin\Controller;

class LoginLogController extends AdminController {

    public function index() {
        $keywords = I('keywords');
        $where = [];

        if ($keywords) {
            $map['u.user_id'] = $keywords;
            $map['u.nick_name'] = array('like', "%{$keywords}%");

            $map['_logic'] = 'or';
            $where['_complex'] = $map;
            $this->assign('keywords', $keywords);
        }

        //日期选择
        $start_date = I("start_date");
        $end_date = I("end_date");


        if (!empty($end_date) && !empty($start_date)) {
            if ($end_date <= $start_date) {
                $this->error('开始时间不允许小于等于结束时间');
                exit;
            } else {
                $where['ll.start_time'] = array('between', array($start_date . ' 00:00:00', $end_date . ' 23:59:59'));
//                $where .= " and create_time between '$start_date 00:00:00' AND '$end_date 23:59:59'";
            }
            $this->assign("start_date", $start_date);
            $this->assign("end_date", $end_date);
        } else {
            if (!empty($start_date)) {
                $where['ll.start_time'] = array('egt', $start_date . ' 00:00:00');
//                $where .= " and create_time >='$start_date 00:00:00'";
                $this->assign("start_date", $start_date);
            }

            if (!empty($end_date)) {
                $where['ll.start_time'] = array('elt', $end_date . ' 23:59:59');
//                $where .= " and create_time <= '$end_date 23:59:59'";
                $this->assign("end_date", $end_date);
            }
        }



        $M = M()->table("ot_log_login ll")
                ->join('ot_users u on ll.user_id =u.user_id');

        $field = 'll.*,u.nick_name';

        $order = "ll.login_log_id desc";
        $list = $this->lists($M, $where, $order, $field);

        if ($list) {
            foreach ($list as &$val) {
                $start = strtotime($val['start_time']);
                if (!empty($val['end_time'])) {
                    $end = strtotime($val['end_time']);
                    $val['min'] = floor(($end - $start) / 60);
                } else {
                    $val['min'] = '-';
                }
            }
        }

        $this->assign('_list', $list);
        $this->display();
    }

}
